﻿Shader "Kun URP/Post Process/BokenBlur"
{
    Properties
    {
        [HideInInspector]_MainTex("MainTex", 2D) = "white" {}
    }
    SubShader
    {
        Tags{
            "RenderPipeline"="UniversalRenderPipeline"
        }
        Cull Off ZWrite Off ZTest Always
        HLSLINCLUDE
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
        CBUFFER_START(UnityPerMaterial)
            float _BlurSmoothness;
            float _BlurRadius;
            float _NearDistance;
            float _FarDistance;
            float _LoopCount;
            float4 _MainTex_TexelSize;
        CBUFFER_END
        TEXTURE2D( _MainTex);
        SAMPLER(sampler_MainTex);
        TEXTURE2D(_SourceTex);
        SAMPLER(sampler_SourceTex);
        TEXTURE2D( _CameraDepthTexture);
        SAMPLER(sampler_CameraDepthTexture);
        struct Attributes
        {
            float4 positionOS       : POSITION;
            float2 uv               : TEXCOORD;
        };
        struct Varyings
        {
            float4 positionCS       : SV_POSITION;
            float2 uv               : TEXCOORD;
        };
        ENDHLSL
        pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            Varyings vert(Attributes input)
            {
                Varyings output;
                output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
                output.uv = input.uv;
                return output;
            }
            half4 frag(Varyings input) : SV_TARGET
            {
                float a = 2.3398;
                float2x2 rotate = float2x2(cos(a), -sin(a), sin(a), cos(a));
                float2 uvPos = float2(_BlurRadius, 0);
                float2 uv;
                float r;
                float4 tex = 0;
                for (int i = 1; i < _LoopCount; i++)
                {
                    r = sqrt(i);
                    uvPos = mul(rotate, uvPos);
                    uv = input.uv + _MainTex_TexelSize.xy * uvPos * r;
                    tex += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
                }
                return tex / (_LoopCount - 1);
            }
            ENDHLSL
        }
        pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            Varyings vert(Attributes input)
            {
                Varyings output;
                output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
                output.uv = input.uv;
                return output;
            }
            half4 frag(Varyings input) : SV_TARGET
            {
                float depth = SAMPLE_TEXTURE2D(_CameraDepthTexture, sampler_CameraDepthTexture, input.uv).x;
                depth = Linear01Depth(depth, _ZBufferParams).x;
                float4 blur = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv);
                float4 sour = SAMPLE_TEXTURE2D(_SourceTex, sampler_SourceTex, input.uv);
                _NearDistance *= _ProjectionParams.w;
                _FarDistance *= _ProjectionParams.w;
                float distance = 1 - smoothstep(_NearDistance, saturate(_NearDistance + _BlurSmoothness), depth);
                distance += smoothstep(_FarDistance, saturate(_FarDistance + _BlurSmoothness), depth);
                // distance = saturate(distance)
                return lerp(sour, blur, saturate(distance));
                // return distance;
            }
            ENDHLSL
        }
    }
}
